package com.geek.bams.dao;

import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

//泛型  任意的大写字母
//T 代表容器中元素的类型，例如：Account
//K 代表容器的类型，例如：Account[] 、 List<Account>
public interface IDao<T , K> {

    public boolean insert(T t) throws IOException;

    public T selectOne(Long id , String password) throws IOException, ClassNotFoundException;

    public T selectOne(Long id);

    public K selectAll();

    //获取ID
    public Long getId();
    //设置ID
    public void setId(Long id);

    //生成账户ID
    public default Long createID() throws IOException {
        //1.获取初始ID值
        String idStr = getId().toString();
        //2.ID值 86215021 2013 02 0001
        Integer year = Integer.valueOf(idStr.substring(8, 12));
        Integer month = Integer.valueOf(idStr.substring(12, 14));
        String count = idStr.substring(14);
        //3.获取当前年月
        Calendar c = Calendar.getInstance();
        int nowYear = c.get(Calendar.YEAR);
        int nowMonth = c.get(Calendar.MONTH)+1;
        if(year!=nowYear || month!=nowMonth){
            year = nowYear;
            month = nowMonth;
            count = "0001";
        }

        //4.判断是否达到9999 提示次月再来
        if(count.equals("9999")){
            count = "0001";
            System.out.println("本月已满，下月再来");
            return -1L;
        }

        //四.组装 862150212025100001
        String str = "";
        if(month<10){
            str = "86215021"+year+"0"+month+count;
        }else{
            str = "86215021"+year+month+count;
        }

        //5.累加计数并返回
        Long id = Long.valueOf(str);
       // setId(id+1);

        return id;
    }

    //默认方法  有利于接口拓展
    public default boolean update(T t) throws IOException {
        return false;
    }

}
